﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Data.OleDb;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows;
using Microsoft.Office.Interop.Excel;

namespace Zuordnung
{
    class zCodeLearn


    {

        #region Learn

        static void learnstopwatch(string[] args)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            Thread.Sleep(10000);
            stopWatch.Stop();
            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            Console.WriteLine(elapsedTime, "RunTime");
        }


        private void LearnAufExcelMitSQLZugreifen()
        {
            String Inhalt = "";
            string fname = "c:\\markus test.xls";
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + fname + "\";Extended Properties=\"Excel 8.0;HDR=YES;\"";
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
            using (DbConnection connection = factory.CreateConnection())
            {
                connection.ConnectionString = connectionString;
                using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandText = "INSERT INTO [Tabelle1$] (Vorname, Nachname) VALUES(\"Tampa\",\"Florida\")";
                    //command.CommandText = "CREATE INDEX IdxVorname ON [Tabelle1$] (Vorname)"; // bad knowhow: kein CREATE INDEX bei xls
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
                using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT Vorname, Nachname FROM [Tabelle1$]";
                    connection.Open();
                    using (DbDataReader dbr = command.ExecuteReader())
                    {
                        while (dbr.Read())
                        {
                            Inhalt += dbr["Vorname"].ToString() + "\n";
                        }
                    }
                    connection.Close();
                }
            }
            MessageBox.Show(Inhalt);
            return;
        }


        #endregion



    }



}
